﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Media;
namespace eKanbanWPF.Model
{
    class Statistics
    {
        public static void GetStatistics(KanbanBoard board, DSStatistics ds)
        {
            //ds.Tables.Add(MemberStats(board));
            //ds.Tables.Add(FeatureStats(board));
            //ds.Tables.Add(TaskStats(board));
            MemberStats(board, ds);
            FeatureStats(board, ds);
            TaskStats(board, ds);
        }

        public static void MemberStats(KanbanBoard board, DSStatistics ds)
        {
            DSStatistics.MemberStatsDataTable table = ds.MemberStats;
            DSStatistics.MemberStatsDetailDataTable detail = ds.MemberStatsDetail;
            table.Clear();
            detail.Clear();
            //DataColumn nameCol = table.Columns.Add("MemberName", Type.GetType("System.String"));
            //DataColumn numOfTaskDoneCol = table.Columns.Add("TaskDone", Type.GetType("System.Int32"));
            //DataColumn timeSpentCol = table.Columns.Add("TimeSpent", Type.GetType("System.Int32"));

            foreach (var member in board.Members)
            {
                //DataRow row = table.NewRow();
                DSStatistics.MemberStatsRow row = table.NewMemberStatsRow();
                //row[table.MemberNameColumn] = member.Name;
                row.MemberName = member.Name;
                //row[table.TaskDoneColumn] = member.History.Count;
                row.TaskDone = member.History.Count;
                int timeSpent = 0;
                foreach (var t in member.History)
                {
                    DSStatistics.MemberStatsDetailRow detailRow = detail.NewMemberStatsDetailRow();
                    detailRow.MemberName = member.Name;
                    detailRow.Task = t.Task.Name;
                    detailRow.StartTime = t.StartTime;

                    if (t.FinishTime != DateTime.MaxValue)
                    {
                        timeSpent += (t.FinishTime - t.StartTime).Days;
                        detailRow.FinishTime = t.FinishTime;
                    }
                    else
                    {
                        timeSpent += (DateTime.Now.Date - t.StartTime).Days;
                    }
                    detail.AddMemberStatsDetailRow(detailRow);
                }
                //row[table.TimeSpentColumn] = timeSpent;
                row.TimeSpent = timeSpent;
                table.AddMemberStatsRow(row);
            }
        }

        public static void FeatureStats(KanbanBoard board, DSStatistics ds)
        {
            DSStatistics.FeatureStatsDataTable table = ds.FeatureStats;
            table.Clear();
            DSStatistics.FeatureStatsDetailDataTable detail = ds.FeatureStatsDetail;
            detail.Clear();
            //var table = new DSStatistics.FeatureStatsDataTable();
            //DataColumn nameCol = table.Columns.Add("FeatureName", Type.GetType("System.String"));
            //DataColumn numOfTaskCol = table.Columns.Add("NumOfTask", Type.GetType("System.Int32"));
            //DataColumn timeSpentCol = table.Columns.Add("TimeSpent", Type.GetType("System.Int32"));

            foreach (var feature in board.Features)
            {
                //DataRow row = table.NewRow();
                DSStatistics.FeatureStatsRow row = table.NewFeatureStatsRow();
                //row[table.FeatureNameColumn] = feature.Name;
                row.FeatureName = feature.Name;
                //row[table.NumOfTaskColumn] = feature.Tasks.Count;
                row.NumOfTask = feature.Tasks.Count;
                int timeSpent = 0;
                foreach (var t in feature.Tasks)
                {
                    DSStatistics.FeatureStatsDetailRow detailRow = detail.NewFeatureStatsDetailRow();
                    detailRow.FeatureName = feature.Name;
                    detailRow.Task = t.Name;
                    detailRow.StartTime = t.StartTime;
                    if (t.FinishTime != DateTime.MaxValue)
                    {
                        timeSpent += (t.FinishTime - t.StartTime).Days;
                        detailRow.StartTime = t.FinishTime;
                    }
                    else
                    {
                        timeSpent += (DateTime.Now.Date - t.StartTime).Days;
                    }
                    detail.AddFeatureStatsDetailRow(detailRow);
                }
                //row[table.TimeSpentColumn] = timeSpent;
                row.TimeSpent = timeSpent;
                table.AddFeatureStatsRow(row);
            }
        }

        public static void TaskStats(KanbanBoard board, DSStatistics ds)
        {
            DSStatistics.TaskStatsDataTable table = ds.TaskStats;
            table.Clear();
            DSStatistics.TaskStatsDetailDataTable detail = ds.TaskStatsDetail;
            detail.Clear();
            //var table = new DSStatistics.TaskStatsDataTable();

            foreach (var task in board.Tasks)
            {
                //var row = table.NewRow();
                DSStatistics.TaskStatsRow row = table.NewTaskStatsRow();
                //((DataRow)row)[table.TaskNameColumn] = task.Name;
                row.TaskName = task.Name;
                //((DataRow)row)[table.FeatureNameColumn] = task.Feature.Name;
                row.FeatureName = task.Feature.Name;

                foreach (var item in task.Doers)
                {
                    DSStatistics.TaskStatsDetailRow detailRow = detail.NewTaskStatsDetailRow();
                    detailRow.TaskName = task.Name;
                    detailRow.Member = item.Name;

                    detail.AddTaskStatsDetailRow(detailRow);
                }

                int timeSpent = 0;
                if (task.FinishTime != DateTime.MaxValue)
                {
                    timeSpent += (task.FinishTime - task.StartTime).Days;
                }
                else
                {
                    timeSpent += (DateTime.Now.Date - task.StartTime).Days;
                }
                //((DataRow)row)[table.TimeSpentColumn] = timeSpent;
                row.TimeSpent = timeSpent;
                table.AddTaskStatsRow(row);
            }
            
        }
    }
}
